package com.cmict.data.system.api.oa;

import com.alibaba.fastjson.JSONObject;
import com.cmict.data.common.websocket.distribute.MessageDO;
import com.cmict.data.framework.datapermission.core.annotation.DataPermission;
import com.cmict.data.framework.tenant.core.aop.TenantIgnore;
import com.cmict.data.framework.tenant.core.context.TenantContextHolder;
import com.cmict.data.system.dal.dataobject.oa.ScheduleManagementDO;
import com.cmict.data.system.dal.mysql.oa.ScheduleManagementMapper;
import com.cmict.data.system.enums.ApiConstants;
import com.cmict.data.system.service.user.AdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = ApiConstants.VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@Slf4j
@DataPermission(enable = false) //忽略数据权限
public class ScheduleManagementApiImpl implements ScheduleManagementApi{

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private ScheduleManagementMapper scheduleManagementMapper;

    @TenantIgnore
    @Override
    public void executeScheduleManagement() {
        MessageDO messageDO = new MessageDO();
        messageDO.setNeedBroadcast(false);   // 是否广播发送
        messageDO.setOnlyOneClientInSameKey(false); // 相同 sessionKey 的客户端只推送一次操作
        // 获取待发送的 sessionKeys
        Collection<Object> sessionKeys  =  adminUserService.getSessionKeys();
        Set<Integer> statusSet = new HashSet<>();
        statusSet.add(0);
        statusSet.add(1);
        //long count = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String LocalDateTime = dateFormat.format(new Date());
        List<ScheduleManagementDO> scheduleManagementlist = scheduleManagementMapper.selectListNoPushAll(statusSet,LocalDateTime);
        for (Object sessionKey : sessionKeys) {
            String key = sessionKey.toString();
            if(key.contains("_")){
                String userId = key.substring(key.indexOf("_")+1 , key.length());
                String tenantId = key.substring(0 , key.indexOf("_"));
                TenantContextHolder.setTenantId(Long.valueOf(tenantId));
                List<ScheduleManagementDO> schedulelist = new ArrayList();
                log.info(tenantId + " scheduleManagement, messageDo: {}", messageDO);
                for (ScheduleManagementDO scheduleManagementDO: scheduleManagementlist) {
                    if(scheduleManagementDO.getCreator().equals(userId) && scheduleManagementDO.getTenantId() == Integer.parseInt(tenantId)){
                        schedulelist.add(scheduleManagementDO);
                        scheduleManagementDO.setStatus(1);
                        scheduleManagementMapper.updateById(scheduleManagementDO);
                    }
                }
                List<Object> sessionKeylist = new ArrayList<>();
                sessionKeylist.add(sessionKey);
                messageDO.setSessionKeys(sessionKeylist);
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("scheduleManagement",schedulelist);
                messageDO.setMessageText(JSONObject.toJSONString(resultMap));
                adminUserService.distribute(messageDO);
            }
        }
    }
}
